AddCSLuaFile("shared.lua")
AddCSLuaFile("cl_init.lua")

include("shared.lua")

function ENT:Initialize()
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetTrigger( true )
	self.Entity:SetSolid(SOLID_VPHYSICS)
	self.Entity:SetUseType(SIMPLE_USE)
    
    self.size = 0
	local phys = self.Entity:GetPhysicsObject()
	if phys:IsValid() then 
	phys:Wake() 
    phys:SetMass( math.ceil( math.pow(phys:GetVolume(),0.5)/100)*10 )
    self.size = math.pow(phys:GetVolume(),0.5)
	end
    
    self.Core = NULL
    self.steamvent = 0
    self.zdest = self.Entity:GetPos().z
    self.zsmooth = self.zdest
    self.up = 0
    self.down = 0
    
    self.maxhp = math.ceil(self.size/100)
    self.hp = self.maxhp
    self.repair = 1
    self.Entity:SetNetworkedFloat("repair", self.repair)
    self.cloth = 1
    
    self.cost = math.ceil(self.size/100)
    self.Repay = 1
    Ent_InitCost( self.Entity )
    
	self.Inputs = Wire_CreateInputs(self, { "Up", "Down" })

end

function ENT:TriggerInput(iname, value)
	if (iname == "Up") then
		self.up = math.Clamp(value,-2,2)
	elseif (iname == "Down") then
        self.down = math.Clamp(value,-2,2)
	end
end
    
function ENT:Think()
    
    local prevrepair = self.repair
    self.repair = math.min(self.repair+0.001,1)
    if(prevrepair==self.repair) then /*comment*/ else self.Entity:SetNetworkedFloat("repair", self.repair) end
    self.cloth = math.Approach(self.cloth,self.repair,0.001)
    
    local phys = self.Entity:GetPhysicsObject()
    if( phys:IsValid() and self.Core:IsValid() ) then
    
        if(not phys:IsMoveable()) then self.zdest = self.Entity:GetPos().z end
    
        self.zdest = self.zdest + (self.up - self.down)*1.5
        local maxforce = math.Clamp(  self.size*self.Core.pressure/50  ,-50000,50000)*self.cloth
        local myz = phys:GetPos().z
        local zvel = phys:GetVelocity().z
        self.zsmooth = Lerp(0.95,myz,self.zdest)
        local force =  (math.min(self.zsmooth - myz,512)*10 - math.Clamp(zvel,-100,100)*3)*maxforce
        
        phys:ApplyForceCenter( Vector(0,0, force ) )
        
        self.steamvent = math.abs((force)/8000)
        
        self.Entity:NextThink( CurTime() + 0.02 )
    else
        self.Entity:NextThink( CurTime() + 1 )
    end
    
    return true
end

function ENT:OnRemove()
	RemoveRepay( self )
end

function ENT:OnRestore()
if WireLib then WireLib.Restored(self) end
end

function ENT:PreEntityCopy()
    if WireLib then
    local DupeInfo = WireLib.BuildDupeInfo(self)
        if DupeInfo then
            duplicator.StoreEntityModifier( self, "WireDupeInfo", DupeInfo )
        end
    end
end

function ENT:PostEntityPaste( Player, Ent, CreatedEntities )
    Ent.Owner = Player
    Ent_InitCost( self.Entity )
    if WireLib and (Ent.EntityMods) and (Ent.EntityMods.WireDupeInfo) then
        WireLib.ApplyDupeInfo(Player, Ent, Ent.EntityMods.WireDupeInfo, function(id) return CreatedEntities[id] end)
    end
end